package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.SetorDAO;
import br.urca.model.CodigoSetor;
import br.urca.model.Setor;

@Transactional
public class JPASetorDAO implements SetorDAO {
	
	private EntityManager manager;
	
	public JPASetorDAO() {
		this(null);
	}
	
	@Inject
	public JPASetorDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<Setor> todos() {
		return this.manager.createQuery("SELECT s FROM Setor s", Setor.class)
				.getResultList();
	}

	@Override
	public Setor buscarPorId(CodigoSetor id) {
		return this.manager.createQuery("SELECT s FROM Setor s "
				+ "WHERE s.id.codigoInstituicao = :codInst "
				+ "AND s.id.codigoSetor = :codSetor ", Setor.class)
				.setParameter("codInst", id.getCodigoInstituicao())
				.setParameter("codSetor", id.getCodigoSetor())
				.getSingleResult();
	}

	

}
